﻿Public Class masdrgdoswuse
    Private DUID = Nothing
    Private DFORM = Nothing
    Private USEID = Nothing
    Private STATUS = Nothing
    Private selectMasdrgdoswuse = "SELECT a.duid as 'รหัสการจับคู่', a.dform AS 'DFORM', a.useid AS 'USEID',  c.dosageform as 'รูปแบบยา', b.dusename as 'รูปแบบการใช้งาน', a.`status` as 'สถานะการเปิดใช้งาน' FROM masdrgdoswuse a JOIN masdrgdosage c ON c.dform = a.dform JOIN masdrguse b ON b.useid = a.useid"
    'Private selectMasdrgdoswuse = "SELECT USEID, DUCODE, DUSENAME, STATUS FROM masdrgdoswuse"
    'Private selectMasdrgacc = "SELECT DGTYID, DRGTYPE, convert(bit, [STATUS]) FROM MASDRGTYPE"

    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Property DUID_ As Integer
        Get
            Return DUID
        End Get
        Set(value As Integer)
            DUID = value
        End Set
    End Property

    Property DFORM_ As Integer
        Get
            Return DFORM
        End Get
        Set(value As Integer)
            DFORM = value
        End Set
    End Property

    Property USEID_ As Integer
        Get
            Return USEID
        End Get
        Set(value As Integer)
            USEID = value
        End Set
    End Property

    Property STATUS_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property

    Property selectMasdrgdoswuse_ As String
        Get
            Return selectMasdrgdoswuse
        End Get
        Set(value As String)

        End Set
    End Property

    Public Sub DFORMData(ByVal comboboxName As ComboBox)
        Dim masdrgdosageData As DataTable
        masdrgdosageData = connect.GetTable("SELECT dform, dosageform FROM masdrgdosage")
        Try
            With comboboxName
                .DataSource = masdrgdosageData
                .DisplayMember = "DOSAGEFORM"
                .ValueMember = "DFORM"
                '.BindingContext = BindingContext
            End With
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub USEIDData(ByVal comboboxName As ComboBox)
        Dim masdrguseData As DataTable
        masdrguseData = connect.GetTable("SELECT useid, dusename FROM masdrguse")
        Try
            With comboboxName
                .DataSource = masdrguseData
                .DisplayMember = "DUSENAME"
                .ValueMember = "USEID"
                '.BindingContext = BindingContext
            End With
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub insertMasdrgdoswuseData()
        If TypeOf DFORM Is Integer And TypeOf USEID Is Integer Then
            If connect.GetTable("SELECT duid FROM masdrgdoswuse WHERE dform = '" & DFORM & "' AND useid = '" & USEID & "'").Rows.Count > 0 Then
                MsgBox("มีข้อมูลวิธีการใช้ยานี้อยู่ในระบบแล้ว")
            Else
                Try
                    connect = CONDB.NewConnection()
                    'connectlocal = conlocal.NewConnection()
                    'Dim dischargeID As Integer

                    connect.ExecuteNonQuery("INSERT INTO masdrgdoswuse (dform, useid, status) VALUES ('" & DFORM & "', '" & USEID & "', '" & STATUS & "')")
                    'dischargeID = connect.ExecuteScalar("INSERT INTO masdischarge (DISCHARGE, STATUS) VALUES ('" & masdischargeDescription & "', '" & masdischargeStatus & "'); SELECT LAST_INSERT_ID();")
                    'connectlocal.ExecuteNonQuery("INSERT INTO MASDISCHARGE (DISCHGID, DISCHARGE, STATUS) VALUES ('" & dischargeID & "', '" & masdischargeDescription & "', '" & masdischargeStatus & "')")
                    MsgBox("ท่านได้ทำการเพิ่มข้อมูลวิธีการใช้ยาเรียบร้อยแล้ว")
                Catch ex As Exception
                    MsgBox(vbCrLf & ex.Message)
                End Try
                connect.Dispose()
                'connectlocal.Dispose()
            End If
        Else
            MsgBox("โปรดกรุณาระบุข้อมูลให้ถูกต้อง")
        End If
    End Sub

    Public Sub editMasdrgdoswuseData()
        If TypeOf DFORM Is Integer And TypeOf USEID Is Integer Then
            Try
                connect = CONDB.NewConnection()
                'connectlocal = conlocal.NewConnection()

                connect.ExecuteNonQuery("UPDATE masdrgdoswuse SET dform = '" & DFORM & "', useid = '" & USEID & "', status = '" & STATUS & "' WHERE duid = '" & DUID & "'")
                'connectlocal.ExecuteNonQuery("UPDATE MASDISCHARGE SET DISCHARGE = '" & masdischargeDescription & "', STATUS = '" & masdischargeStatus & "' WHERE DISCHGID = '" & masdischargeID & "'")
                MsgBox("ท่านได้ทำการแก้ไขข้อมูลวิธีการใช้ยาเรียบร้อยแล้ว")
            Catch ex As Exception
                MsgBox(vbCrLf & ex.Message)
            End Try
            connect.Dispose()
            'connectlocal.Dispose()
        Else
            MsgBox("โปรดกรุณาระบุข้อมูลให้ถูกต้อง ต้องเป็นตัวอักษร")
        End If
    End Sub
End Class
